package com.xzk.service.impl;

import com.xzk.bean.Express;
import com.xzk.dao.ExpressDao;
import com.xzk.exception.DuplicateNumberException;
import com.xzk.service.ExpressService;
import com.xzk.util.RandomCodeUtil;
import com.xzk.util.SMSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ExpressServiceImpl
 * @Description 快递Service层
 * @Author 0715-YuHao
 * @Date 2020/10/6 12:46
 * @Version 1.0
 */
@Service("expressService")
public class ExpressServiceImpl implements ExpressService {

    @Autowired
    private ExpressDao dao;

    @Override
    public Map<String, Integer> console() {
        return dao.console();
    }

    @Override
    public List<Express> findAll() {
        return dao.findAll();
    }

    @Override
    public Express findByNumber(String number) {
        return dao.findByNumber(number);
    }

    @Override
    public Express findByCode(String code) {
        return dao.findByCode(code);
    }

    @Override
    public List<Express> findByUserPhone(String userPhone) {
        return dao.findByUserPhone(userPhone);
    }

    @Override
    public List<Express> findByUserPhoneAndStatus(String userPhone, int status) {
        Map map = new HashMap();
        map.put("userPhone", userPhone);
        map.put("status", status);
        return dao.findByUserPhoneAndStatus(map);
    }

    @Override
    public List<Express> findBySysPhone(String sysPhone) {
        return dao.findBySysPhone(sysPhone);
    }

    @Override
    public boolean insert(Express express) throws DuplicateNumberException {
        //1.生成取件码
        express.setCode(RandomCodeUtil.getCode());
        try {
            //2.录入快递
            int insert = dao.insert(express);
            if (insert > 0) {
                //3.录入成功，发送短信
                SMSUtil.send(express.getUserPhone(), express.getCode());
                return true;
            }
        }catch (Exception e) {
            if (e.getMessage().endsWith("for key 'CODE'")) {
                insert(express);
            }else if (e.getMessage().endsWith("for key 'number'")) {
                throw new DuplicateNumberException(e.getMessage());
            }else {
                e.printStackTrace();
            }
        }
        return false;
    }

    @Override
    public boolean update(int id, Express e) throws DuplicateNumberException {
        if (e.getUserPhone() != null) {
            dao.delete(id);
            return insert(e);
        }else {
            Map map = new HashMap();
            map.put("id", id);
            map.put("number", e.getNumber());
            map.put("username", e.getUsername());
            map.put("company", e.getCompany());
            map.put("status", e.getStatus());
            Express e1 = dao.findByNumber(e.getNumber());
            if (e.getStatus() == 1) {
                updateStatus(e1.getCode());
            }
            return dao.update(map) > 0;
        }
    }

    @Override
    public boolean updateStatus(String code) {
        return dao.updateStatus(code) > 0;
    }

    @Override
    public boolean delete(int id) {
        return dao.delete(id) > 0;
    }
}
